﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CompanyManagementDLL.MetaData;

namespace CompanyManagementDLL.DLL
{
    public class ModuleDLL
    {
        private readonly CompanyMgtEntities db = new CompanyMgtEntities();
        #region Fecth Data
        /// <summary>
        /// Return module detail based in module id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ModuleMetaData GetModuleByID(int id)
        {
            ModuleMetaData modulemetadata = new ModuleMetaData();
            try
            {
                modulemetadata = db.Modules.Where(r => r.ModuleID == id).Select(u => new ModuleMetaData
                {
                    ModuleName = u.ModuleName,
                    ModuleID = u.ModuleID,
                    ProjectID = u.ProjectID,
                    CreatedBy = u.CreatedBy,
                    CreatedDate = u.CreatedDate,
                    IsActive = u.IsActive ?? false,
                    IsArchive = u.IsArchive,

                }).FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return modulemetadata;
        }

        #endregion

        #region Get List

        /// <summary>
        /// Retrun module list
        /// </summary>
        /// <returns></returns>
        public List<ModuleMetaData> GetModuleList()
        {
            List<ModuleMetaData> ModuleMetaData = new List<ModuleMetaData>();
            try
            {
                ModuleMetaData = db.Modules.Where(u => u.IsActive == true && u.IsArchive == false).Select(u => new ModuleMetaData
                {
                    ModuleName = u.ModuleName,
                    ModuleID = u.ModuleID,
                    ProjectID = u.ProjectID,
                    CreatedBy = u.CreatedBy,
                    CreatedDate = u.CreatedDate,
                    IsActive = u.IsActive ?? false,
                    IsArchive = u.IsArchive,
                }).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ModuleMetaData;
        }

        #endregion

        #region DML Opration

        /// <summary>
        /// insert module entity
        /// </summary>
        /// <param name="ModuleMetaData"></param>
        public void InsertModule(ModuleMetaData ModuleMetaData)
        {
            Module tblModule = new Module();
            try
            {
                tblModule.ModuleName = ModuleMetaData.ModuleName;
                tblModule.IsActive = ModuleMetaData.IsActive;
                tblModule.IsArchive = false;
                tblModule.ProjectID = ModuleMetaData.ProjectID;
                tblModule.CreatedBy = ModuleMetaData.CreatedBy;
                tblModule.CreatedDate = DateTime.Now;
                db.Modules.Add(tblModule);
                db.SaveChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// update module entity
        /// </summary>
        /// <param name="ModuleMetaData"></param>
        public void UpdateModule(ModuleMetaData ModuleMetaData)
        {
            Module tblModule = new Module();
            try
            {
                tblModule.ModuleName = ModuleMetaData.ModuleName;
                tblModule.IsActive = ModuleMetaData.IsActive;
                tblModule.ProjectID = ModuleMetaData.ProjectID;
                tblModule.IsArchive = ModuleMetaData.IsArchive == true ? ModuleMetaData.IsArchive : false;
                db.SaveChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// delete task entity
        /// </summary>
        /// <param name="id"></param>
        public void DeleteModule(int id)
        {
            try
            {
                Module tblModule = new Module();
                tblModule = db.Modules.Where(r => r.ModuleID == id).FirstOrDefault();
                tblModule.IsArchive = true;
                db.SaveChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        #endregion
    }
}
